package xyz.xiaoshengfu.xrpc.spring;

import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;

import xyz.xiaoshengfu.xrpc.proxy.XRpcProxyClient;

/**
 * <p>Title: XrpcServiceFactory</p>
 * <p>Description: 接口实例工厂，这里主要是用于提供接口的实例对象</p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年3月23日
 */
@SuppressWarnings("unchecked")
public class XrpcServiceFactory<T> implements FactoryBean<T> {

	private Class<T> interfaceType;

	public XrpcServiceFactory(Class<T> interfaceType) {
		this.interfaceType = interfaceType;
	}

	@Override
	public T getObject() throws Exception {
		return (T) Proxy.newProxyInstance(interfaceType.getClassLoader(), new Class[] { interfaceType },
				new XRpcProxyClient(interfaceType.getName()));
	}

	@Override
	public Class<T> getObjectType() {
		return interfaceType;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}